﻿using System;
using System.Windows;
using System.Windows.Markup;
using Creventive.Wpf.ShellFactory.Logging;
using Creventive.Wpf.ShellFactory.Workspace.Primitives;

namespace Creventive.Wpf.ShellFactory.Workspace
{
    /// <summary>
    ///   Represents a single application view with a view model of a given type.
    /// </summary>
    /// <typeparam name = "T"></typeparam>
    [ContentProperty("Content")]
    public class ApplicationView<T> : ApplicationView
        where T : class
    {
        /// <summary>
        ///   Initializes the <see cref = "ApplicationView&lt;T&gt;" /> class.
        /// </summary>
        static ApplicationView()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ApplicationView<T>), new FrameworkPropertyMetadata(typeof(ApplicationView)));
        }

        /// <summary>
        ///   Gets the view model.
        /// </summary>
        /// <value>The view model.</value>
        public new T ViewModel
        {
            get { return this.DataContext as T; }
        }

        /// <summary>
        ///   Gets the data type of the view model.
        /// </summary>
        /// <value>The type of the view model.</value>
        public override Type ViewModelType
        {
            get { return typeof(T); }
        }

        /// <summary>
        /// Occurs when the view has opened (i.e. the first time the Loaded event fires)
        /// </summary>
        protected override void OnOpened()
        {
            Logger.Default.Info("Opened view of type " + this.GetType().FullName);
            base.OnOpened();
        }

        /// <summary>
        /// Called when the view has been closed.
        /// </summary>
        protected override void OnClosed()
        {
            base.OnClosed();
            Logger.Default.Info("Closed view of type " + this.GetType().FullName);
        }
    }
}